package org.example.everyday;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2022/4/20 14:30
 */
public class Lc388 {
    public static void main(String[] args) {
        Lc388 lc388 = new Lc388();

       String input = "a\n\taa\n\t\taaa\n\t\t\tfile1.txt\naaaaaaaaaaaaaaaaaaaaa\n\tsth.png";
       int res = lc388.lengthLongestPath(input);
        System.out.println(res);
    }

    public int lengthLongestPath(String input) {
        int n = input.length();
        int pos = 0;
        int ans = 0;
        Deque<Integer> stack = new ArrayDeque<Integer>();

        while (pos < n){
            int depth = 1;
            while (pos < n && input.charAt(pos) == '\t'){
                pos++;
                depth++;
            }

            boolean isFile = false;
            int len = 0;
            while (pos < n && input.charAt(pos) != '\n'){
                if(input.charAt(pos) == '.'){
                    isFile = true;
                }
                pos++;
                len++;
            }

            //跳过换行符
            pos++;

            while (stack.size() >= depth){
                stack.pop();
            }
            if(!stack.isEmpty()){
                len += stack.peek() + 1;
            }
            if(isFile){
                ans = Math.max(ans,len);
            }else{
                stack.push(len);
            }

        }
        return ans;
    }


}
